package client;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.HeadlessException;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTable;
import javax.swing.JButton;
import java.awt.Font;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableModel;

import server.BkLAccITF;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

import client.Configuration;

public class BkLoanAccountGUI extends JFrame {

	private JPanel contentPane;
	private JTable table;
	Person ps = new Person();
	int pID = ps.getPID();
	
	
	/**
	 * Launch the application.
	 */
	private Object[][] LAcc;
	
	Configuration config=new Configuration();
	String rmiAddress=config.getRmiAddress();
	BkLAccITF LAitf;
	
	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					BkLoanAccountGUI frame = new BkLoanAccountGUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public BkLoanAccountGUI() {
		setTitle("\u8D37\u6B3E\u8D26\u6237");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel label = new JLabel("\u5DF2\u6709\u8D37\u6B3E\u9879\uFF1A");
		label.setFont(new Font("微软雅黑", Font.PLAIN, 15));
		label.setBounds(33, 27, 97, 18);
		contentPane.add(label);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(43, 63, 368, 106);
		contentPane.add(scrollPane);
		
		
		try {
			LAitf = (BkLAccITF) Naming.lookup(rmiAddress+"BankLoanAccount");
			int j=LAitf.getpSize(pID);
			LAcc=new Object[j][];//////得到行
			
			int laNum=LAitf.getFirstLANum(pID);
			int i=0;
			while(laNum!=-1){
			    LAcc[i]=new Object[4];
				LAcc[i][0]=new Integer(laNum);
				LAcc[i][1]=new Double(LAitf.getLASum(pID,laNum));
				LAcc[i][2]=LAitf.getStartTime(pID,laNum);
				LAcc[i][3]=new Integer(LAitf.getLimit(pID,laNum));
				i++;
				laNum=LAitf.getNextLANum(pID, laNum);
			}
		} catch (MalformedURLException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		} catch (RemoteException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		} catch (NotBoundException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		
			
		table = new JTable();
		table.setModel(new DefaultTableModel(
				LAcc,
				
			new String[] {
				"\u8D37\u6B3E\u53F7", "\u8D37\u6B3E\u989D", "\u8D77\u59CB\u65F6\u95F4", "\u5E74\u9650"
			}
		));
		scrollPane.setViewportView(table);
		
		JButton button_1 = new JButton("\u8FD8\u6B3E");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					LAitf = (BkLAccITF) Naming.lookup(rmiAddress+"BankLoanAccount");
					int a[]=table.getSelectedRows();
					int LANum=(Integer)table.getValueAt(a[0], 0);
					dispose();
					if(LAitf.payBack(pID, LANum)==true)
		
					JOptionPane.showMessageDialog(null, "还款成功！");
					
				else{
					JOptionPane.showMessageDialog(null, "您的活期账户余额不足，还款失败！");
					}
					BkLoanAccountGUI frame = new BkLoanAccountGUI();
					frame.setLocationRelativeTo(null);
					frame.setVisible(true);
					
					
				} catch (MalformedURLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (RemoteException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (NotBoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
				
				}
		});
		button_1.setFont(new Font("微软雅黑", Font.PLAIN, 15));
		button_1.setBounds(287, 190, 97, 39);
		contentPane.add(button_1);
		
		JButton button_3 = new JButton("\u67E5\u770B\u5229\u606F");
		button_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				try {
					LAitf = (BkLAccITF) Naming.lookup(rmiAddress+"BankLoanAccount");
					int a[]=table.getSelectedRows();
					int LANum=(Integer)table.getValueAt(a[0], 0);
					
					JOptionPane.showMessageDialog(null, LAitf.checkInterest(pID, LANum));
				} catch (HeadlessException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (RemoteException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (MalformedURLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (NotBoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		button_3.setFont(new Font("微软雅黑", Font.PLAIN, 15));
		button_3.setBounds(180, 190, 97, 39);
		contentPane.add(button_3);
		
		JButton button_4 = new JButton("\u65B0\u589E\u8D37\u6B3E");
		button_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				BkAddLAccGUI frame = new BkAddLAccGUI();
				frame.setLocationRelativeTo(null);
				frame.setVisible(true);
				dispose();
			}
		});
		button_4.setFont(new Font("微软雅黑", Font.PLAIN, 15));
		button_4.setBounds(63, 190, 107, 39);
		contentPane.add(button_4);
		
		JButton button = new JButton("\u8FD4\u56DE");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				BankAccountGUI frame = new BankAccountGUI();
				frame.setLocationRelativeTo(null);
				frame.setVisible(true);
				dispose();
			}
		});
		button.setFont(new Font("微软雅黑", Font.PLAIN, 13));
		button.setBounds(341, 10, 83, 22);
		contentPane.add(button);
	}
}

